package com.wechat.service.storage;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;

import com.swak.App;
import com.swak.Constants;
import com.swak.codec.Digests;
import com.swak.codec.Encodes;
import com.swak.utils.time.DateTimes;
import com.wechat.login.Login;
import com.wechat.login.LoginService;
import com.wechat.service.StorageService;

import lombok.Getter;

public abstract class SubjectStorageService {

	@Autowired
	protected StorageService storageService;
	@Autowired
	protected App app;
	@Getter
	protected final Login login;

	public SubjectStorageService() {
		this.login = LoginService.login();
	}

	public SubjectStorageService(StorageService storageService, App app) {
		this();
		this.storageService = storageService;
		this.app = app;
	}

	protected File getStoragePath() {
		return storageService.getStoragePath();
	}

	protected String genFileDateDirName() {
		return DateTimes.getFormatNow(Constants.Day_Format);
	}

	protected String genFilePrefDirName(String name) {
		return name.substring(0, 2);
	}

	protected Long genFileID() {
		return App.genId();
	}

	protected String genFileIDByName(String name) {
		return Digests.md5(name);
	}

	protected String genFileIDByContent(byte[] content) {
		return Encodes.encodeHex(Digests.md5(content));
	}
}
